﻿function AzureOnlineTableCreateDetailsView(context) {

    var dispatcher = $({});
    var name = ko.observable('');
    var triedToSubmit = ko.observable(false);

    var regex = new RegExp('^[A-Za-z][A-Za-z0-9]{2,62}$');

    var validName = ko.computed(function () {
        if (!triedToSubmit()) return true;
        var n = name().trim();
        return n.match(regex);
    });

    var showNameRequired = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return true;
        return false;
    });

    var showMustContainAlphaNumericsOnly = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        for (var i = 0; i < n.length; i++) {
            var ch = n.charAt(i);
            if (!utils.isAlpha(ch) && !utils.isDigit(ch)) {
                return true;
            }
        }
        return false;
    });

    var showMustStartWithLetter = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        var ch = n.charAt(0);
        if (utils.isAlpha(ch)) {
            return false;
        }
        return true;
    });

    var showLength = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        if (n.length < 3) return true;
        if (n.length > 63) return true;
        return false;
    });

    var spaRuntime = context.spaRuntime;

    var tableDetailsRoute = spaRuntime.findRouteByName('tableRecords');

    function reset() {
        triedToSubmit(false);
        name('');
    }

    function validate() {
        triedToSubmit(true);
        return validName();
    }

    function create() {
        if (validate()) {
            var api = context.api();
            var table = name();
            var content = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
            content += '<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"';
            content += '  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"';
            content += '  xmlns="http://www.w3.org/2005/Atom">';
            content += '  <title /> ';
            content += '  <updated>2009-03-18T11:48:34.9840639-07:00</updated>';
            content += '  <author>';
            content += '  <name/> ';
            content += '  </author> ';
            content += '  <id/> ';
            content += '  <content type="application/xml">';
            content += '    <m:properties>';
            content += '      <d:TableName>' + table + '</d:TableName>';
            content += '    </m:properties>';
            content += '  </content> ';
            content += '</entry>';

            api.tables.createTable({
                table: table,
                content: content,
                success: function () {
                    var newItem = new AzureOnlineTableEntry();
                    newItem.name(table);
                    context.tables.addNewTable(newItem);
                    reset();
                    dispatcher.trigger('success');
                    window.location.hash = tableDetailsRoute.formatHash({
                        tableName: table
                    });
                }
            });
        }
    }

    this.name = name;
    this.validName = validName;
    this.hasErrors = ko.computed(function () {
        return !validName();
    });
    this.showNameRequired = showNameRequired;
    this.showMustStartWithLetter = showMustStartWithLetter;
    this.showMustContainAlphaNumericsOnly = showMustContainAlphaNumericsOnly;
    this.showLength = showLength;
    this.reset = reset;
    this.create = create;

    this.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}